package com.javabean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.model.DataBase;

public class UpdateDevice {
	PreparedStatement pre;
	Connection con = DataBase.getConnection();
	java.util.Date enddate;
	java.util.Date begindate;
	boolean flag = false;
	boolean isexist = false;
	ResultSet rs;

	public boolean isexist(String type) {
		String sql = "select * from device_type where type=?";
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			rs = pre.executeQuery();
			if (rs.next()) {
				isexist = true;
			} else {
				isexist = false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return isexist;

	}

	public boolean updateDevice(String type, int serial, String model,
			String department, String producter, String edate, String mdate,
			String extracontent, String devpro, long did) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			enddate = sdf.parse(edate);
			begindate = sdf.parse(mdate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String sql = "update device set type=?,model=?,department=?,serial=?,edate=?,mdate=?,vender=?,remark=?,username=? where did=?";
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, type);
			pre.setString(2, model);
			pre.setString(3, department);
			pre.setInt(4, serial);
			pre.setDate(5, new java.sql.Date(enddate.getTime()));
			pre.setDate(6, new java.sql.Date(begindate.getTime()));
			pre.setString(7, producter);
			pre.setString(8, extracontent);
			pre.setString(9,devpro);
			pre.setLong(10, did);
			pre.executeUpdate();
			flag = true;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;

	}

}
